﻿using Jack.EtcdConfiguration.WebApiDemo.Options;

namespace Jack.EtcdConfiguration.WebApiDemo
{
    public class Startup
    {
        public IWebHostEnvironment Env { get; set; }

        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration, IWebHostEnvironment env)
        {
            Configuration = configuration;
            Env = env;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddEndpointsApiExplorer();
            services.AddSwaggerGen();

            services.Configure<RedisOptions>(Configuration.GetSection("RedisOptions"));
            services.Configure<EfCoreOptions>(Configuration.GetSection("EfCoreOptions"));
            services.Configure<AppSettingOptions>(Configuration.GetSection("AppSettingOptions"));
        }

        public void Configure(IApplicationBuilder app, IHostApplicationLifetime hostApplicationLifetime)
        {
            if (Env.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            EtcdConfigurationProvider.CancellationToken = hostApplicationLifetime.ApplicationStopping;
        }
    }
}
